package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.service.IBaseGradeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by wens on 2017/6/16.
 */
@Controller
public class ServerStatusController {

    @Resource
    private RedisTemplate redisTemplate ;

    @Resource
    private IBaseGradeService baseGradeService ;


    @ApiOperation("监控")
    @RequestMapping(value = "/server/check", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String check() {

        //检查redis
        redisTemplate.opsForValue().set("check:at" , String.valueOf(System.currentTimeMillis()));

        Example<BaseGrade> example = BaseGrade.createExample();
        BaseGrade one = baseGradeService.findOneByExample(example);

        if(one == null){
            throw new BusException("Not Good");
        }

        return "ok2" ;


    }

    @ApiOperation("jvm")
    @RequestMapping(value = "/server/jvm", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String jvm() {
        Runtime runtime = Runtime.getRuntime();

        return String.format("hostIP:%s,maxMemory:%s,totalMemory:%s,freeMemory:%s", Utils.getHostIP(),runtime.maxMemory() ,runtime.totalMemory(),runtime.freeMemory() ) ;
    }

    @RequestMapping(value = "/server/test", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String test( @RequestParam(value = "d" ,defaultValue = "30000") long d ) throws UnknownHostException {



        return "ok" ;
    }


}
